// pages/manager/manager.js

const config = require("../../utils/config.js")
const api = require('../../utils/api.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // unlock_requests:[{"device_name":"sgxkb12121", "time":"2019-01-20 17:30:01", "phone":"18915327209"}, 
    //   { "device_name": "sgxkb12122", "time": "2019-01-20 17:32:01", "phone": "18915327209" },
    //   { "device_name": "sgxkb12123", "time": "2019-01-20 17:34:01", "phone": "18915327209" },
    //   { "device_name": "sgxkb12124", "time": "2019-01-20 17:35:01", "phone": "18915327209" }],

    request_style:"request_block",
    unlock_requests:[],
  },


  // data: {
  //   currentIndex: 0,
  //   "firstList": ["LXT", "LXT", "LXT", "LXT", "LXT", "LXT"],
  //   "secondList": ["GFF", "GFF", "GFF", "GFF", "GFF", "GFF", "GFF", "GFF"]
  // },
  //swiper切换时会调用
  // pagechange: function (e) {
  //   if ("touch" === e.detail.source) {
  //     let currentPageIndex = this.data.currentIndex
  //     currentPageIndex = (currentPageIndex + 1) % 2
  //     this.setData({
  //       currentIndex: currentPageIndex
  //     })
  //   }
  // },
  // //用户点击tab时调用
  // titleClick: function (e) {
  //   let currentPageIndex =
  //     this.setData({
  //       //拿到当前索引并动态改变
  //       currentIndex: e.currentTarget.dataset.idx
  //     })
  // },


  onClickOneRequest: function(e) {
    var index = e.currentTarget.dataset.rindex
    console.log("clicked index: ", index)
    console.log("clicked item: ", this.data.unlock_requests[index])

    var dataClicked = this.data.unlock_requests[index]
    var strData = dataClicked['device_name'] + ';' + dataClicked['phone'] + ';' + dataClicked['lastId']

    wx.navigateTo({
      url: '../oneRequest/oneRequest?data=' + strData,
    })
  },

  onSetting: function() {
    wx.navigateTo({
      url: '../setting/setting',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    setInterval(
      function () {
        if (that.data.request_style == 'request_block') {
          that.setData({
            request_style: "request_block_red"
          })
        }
        else {
          that.setData({
            request_style: "request_block"
          })
        }
      }, 800
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var openid = wx.getStorageSync('openid')
    api.get('getRequestUnlock', { 'openid': openid }).then(res => {
      console.log("onManager res: ", res)
        that.setData({
          unlock_requests:res
        })
    })
  },

  onRefreshRequest: function() {
    var that = this
    var openid = wx.getStorageSync('openid')
    api.get('getRequestUnlock', { 'openid': openid }).then(res => {
      console.log("onManager res: ", res)
      that.setData({
        unlock_requests: res
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})